<template>
  <basic-container>
    <XmTable :config="tableConfig" :noWatchParams="noWatchParams" class="table" ref="table" row-key="id" :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <div slot="filter">
        <el-form class="fl" :inline="true" ref="form" :model="noWatchParams">
          <el-form-item label="字典编号">
            <el-input v-model.trim="noWatchParams.code" size="small" placeholder="字典编号"></el-input>
          </el-form-item>
          <el-form-item label="字典名称">
            <el-input v-model.trim="noWatchParams.dictValue" size="small" placeholder="字典名称"></el-input>
          </el-form-item>
          <el-form-item label="字典备注">
            <el-input v-model.trim="noWatchParams.remark" size="small" placeholder="字典备注"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              @click="search"
              size="small"
              icon="el-icon-search"
              style="margin-left: 10px;"
            >搜索</el-button>
            <el-button
              @click="reset"
              size="small"
              icon="el-icon-delete"
              style="margin-left: 10px;"
            >清空</el-button>
          </el-form-item>
        </el-form>
        <div class="fr">
          <el-button icon="el-icon-plus" size="small" type="danger" @click="add" v-if="isAddBtn">新增</el-button>
        </div>
      </div>

      <template slot="column-operate" slot-scope="{data}">
        <el-button type="text" icon="el-icon-edit" size="small" @click="edit(data)" v-if="getIsEdit(data)">
          编辑
        </el-button>
        <el-button type="text" icon="el-icon-view" size="small" @click="view(data)" v-if="isViewBtn">
          查看
        </el-button>
        <el-button type="text" icon="el-icon-delete" size="small" @click="remove(data)" v-if="isDelBtn">
          删除
        </el-button>
      </template>
    </XmTable>
    
    <addDictDialog ref="addDictDialog" @submit="search"></addDictDialog>
  </basic-container>
</template>

<script>
import {remove} from "@/api/system/dict";
import tableConfig from './config/table'
import addDictDialog from './addDictDialog'
import {mapGetters} from "vuex";
export default {
  data() {
    return {
      tableConfig,
      noWatchParams: {
        code: '',
        dictValue: '',
        remark: '',
      },
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    isAddBtn() {
      return this.permission.dict_add
    },
    isViewBtn() {
      return this.permission.dict_view
    },
    isDelBtn() {
      return false
    },
    isEditBtn() {
      return this.permission.dict_edit
    },
  },
  methods: {
    search() {
      this.$refs.table.fetch()
    },
    reset() {
      this.noWatchParams = {
        code: '',
        dictValue: '',
        remark: '',
      }
      this.$nextTick(() => {
        this.$refs.table.fetch()
      })
    },
    add() {
      this.$refs.addDictDialog.show({type: 'add', dictList: this.$refs.table.fetchData})
    },
    edit(data) {
      this.$refs.addDictDialog.show({type: 'edit', data, dictList: this.$refs.table.fetchData})
    },
    view(data) {
      this.$refs.addDictDialog.show({type: 'view', data, dictList: this.$refs.table.fetchData})
    },
    remove(data) {
      this.$confirm('请确认是否删除该记录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        remove(data.id).then(res => {
          if (res.data.code === 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.search()
          } else {
            this.$message({
              type: 'error',
              message: res.data.msg
            });
          }
        })
      })
    },
    getIsEdit(data) {
      return (data.ancestorCode === 'complaint_department' || data.ancestorCode === 'refund_reason' || data.ancestorCode === 'delivery_fee') && this.permission.dict_edit
    }
  },
  components: {
    addDictDialog
  }
};
</script>
<style lang="scss" scoped>
</style>